package com.quanyan.authority.service.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.authority.mapper.SystemMapper;
import com.quanyan.authority.mapper.TbSystemMapper;
import com.quanyan.authority.model.db.TbSystem;
import com.quanyan.authority.model.request.ReqBaseParam;
import com.quanyan.authority.model.request.ReqPage;
import com.quanyan.authority.service.SystemService;
import com.quanyan.common.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/1.
 */
@Service
public class SystemServiceImpl implements SystemService{

    @Autowired
    private TbSystemMapper tbSystemMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Override
    public TbSystem findById(Integer id) {
        if(id!=null)
            return tbSystemMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public TbSystem add(ReqBaseParam reqBaseParam) {
        TbSystem tbSystem = new TbSystem();
        tbSystem.setCode(reqBaseParam.getCode());
        tbSystem.setName(reqBaseParam.getName());
        tbSystem.setRemark(reqBaseParam.getRemark());
        tbSystem.setCreateTime(new Date());
        tbSystem.setUpdateTime(new Date());
        tbSystem.setIsDelete(Constants.IS_DELETE_FALSE);
        tbSystem.setFounder(reqBaseParam.getFounder());
        tbSystemMapper.insertSelective(tbSystem);
        return tbSystem;
    }

    @Override
    public void del(Integer id) {
        if(id!=null)
            tbSystemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ReqBaseParam reqBaseParam) {
        TbSystem tbSystem = new TbSystem();
        tbSystem.setId(reqBaseParam.getId());
        tbSystem.setCode(reqBaseParam.getCode());
        tbSystem.setName(reqBaseParam.getName());
        tbSystem.setRemark(reqBaseParam.getRemark());
        tbSystem.setUpdateTime(new Date());
        tbSystem.setIsDelete(reqBaseParam.getIsDelete().byteValue());
        tbSystemMapper.updateByPrimaryKeySelective(tbSystem);
    }

    @Override
    public Page findAll(ReqPage reqPage, ReqBaseParam reqBaseParam) {
        Page page = new Page();
        page.setCurrentPage(reqPage.getPageNum());
        page.setShowCount(reqPage.getPageSize());
        List<TbSystem> lst = systemMapper.findAll(page, reqBaseParam.toMap());
        page.setObject(lst);
        return page;
    }
}
